package cl.jpavlich.mpm.view;

import java.awt.BorderLayout;
import java.awt.Dimension;

import javax.swing.JDialog;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;

import org.metawidget.inspector.annotation.UiAction;
import org.metawidget.inspector.annotation.UiComesAfter;
import org.metawidget.swing.Facet;
import org.metawidget.swing.SwingMetawidget;
import org.metawidget.swing.layout.FlowLayout;

import cl.jpavlich.mpm.model.Command;
import cl.jpavlich.mpm.model.Copy;
import cl.jpavlich.mpm.model.Download;
import cl.jpavlich.mpm.model.ModPackage;
import cl.jpavlich.mpm.services.Bundle;
import cl.jpavlich.mpm.services.PersistenceUtils;
import cl.jpavlich.mpm.view.metawidgets.ListTableModel;

public class InstallationEditor extends JDialog {
	private static final long serialVersionUID = -1440627077393257229L;
	private JTable tableInstallationSteps;
	private ListTableModel<Command> installationCommands;
	private ModPackage modPackage = PersistenceUtils.createModPackage(0);
	private SwingMetawidget mwButtons;
	private EditDialog editDialog = new EditDialog();
	

	/**
	 * Create the dialog.
	 */
	public InstallationEditor() {
		setBounds(100, 100, 800, 435);
		getContentPane().setLayout(new BorderLayout(0, 0));
		installationCommands = new ListTableModel<Command>(Command.class, modPackage.getInstallation(), Bundle.getInstance(), "CommandDescription");
		tableInstallationSteps = new JTable(installationCommands);
		tableInstallationSteps.setPreferredScrollableViewportSize(new Dimension(500,400));
		tableInstallationSteps.setSelectionMode(ListSelectionModel.SINGLE_SELECTION );

		JScrollPane scrollPane = new JScrollPane(tableInstallationSteps);
		getContentPane().add(scrollPane, BorderLayout.CENTER);
		
		mwButtons = GUIUtils.createMetaWidget(false);
		mwButtons.setConfig("cl/jpavlich/mpm/view/config/metawidget.xml");
		mwButtons.setMetawidgetLayout(new FlowLayout());
		mwButtons.setToInspect(this);
		
		Facet f = new Facet();
		f.setName("buttons");
		f.setOpaque(false);
		f.add(mwButtons);
		
		getContentPane().add(f,BorderLayout.SOUTH);
		setTitle(Bundle.getInstance().getString("installation.editor"));
		GUIUtils.center(this);
	}
	
	public void edit(ModPackage modPackage) {
		this.modPackage = modPackage;
		refresh();
		mwButtons.setToInspect(this);
		setVisible(true);
	}

	/**
	 * 
	 */
	private void refresh() {
		installationCommands = new ListTableModel<Command>(Command.class, this.modPackage.getInstallation(), Bundle.getInstance(), "CommandDescription");
		tableInstallationSteps.setModel(installationCommands);
	}

	private void addCommand(Command cmd) {
		modPackage.getInstallation().add(cmd);
		refresh();
	}
	
	
	@UiAction
	public void addCopyCommand() {
		addCommand(new Copy("", "", modPackage));
	}

	@UiAction
	@UiComesAfter("addCopyCommand")
	public void addDownloadCommand() {
		addCommand(new Download(""));
	}
	
	@UiAction
	@UiComesAfter("addDownloadCommand")
	public void editCommand() {
		if (tableInstallationSteps.getSelectedRow() > -1) {
			editDialog.edit(installationCommands.getValueAt(tableInstallationSteps.getSelectedRow()));
			refresh();
		}
		
	}

	@UiAction
	@UiComesAfter("editCommand")
	public void moveUp() {
		int row = tableInstallationSteps.getSelectedRow();
		if (row > 0) {
			Command c = modPackage.getInstallation().remove(row);
			modPackage.getInstallation().add(row-1,c);
			refresh();
			tableInstallationSteps.setRowSelectionInterval(row-1, row-1);
		}		
	}
	
	@UiAction
	@UiComesAfter("moveUp")
	public void moveDown() {
		int row = tableInstallationSteps.getSelectedRow();
		if (row > -1 && row < modPackage.getInstallation().size()-1) {
			Command c = modPackage.getInstallation().remove(row);
			modPackage.getInstallation().add(row+1,c);
			refresh();
			tableInstallationSteps.setRowSelectionInterval(row+1, row+1);
		}		
	}
	
	@UiAction
	@UiComesAfter("moveDown")
	public void deleteCommand() {
		if (tableInstallationSteps.getSelectedRow() > -1) {
			modPackage.getInstallation().remove(tableInstallationSteps.getSelectedRow());
			refresh();
		}
	}
	

	
	@UiAction 
	@UiComesAfter("deleteCommand")
	public void done() {
		modPackage.getInstallation().clear();
		modPackage.getInstallation().addAll(installationCommands.exportCollection());
		setVisible(false);
	}

}
